#include "interrupt.h"
#include "os_constant.h"
#include "asm_utils.h"

void InterruptManager::initialize()
{

    // 初始化IDT

    IDT = (uint32 *)IDT_START_ADDRESS;

    asm_lidt(IDT_START_ADDRESS, 256 * 8 - 1);


    for (uint i = 0; i < 256; ++i)

    {

        setInterruptDescriptor(i, (uint32)asm_interrupt_empty_handler, 0);

    }

}

// 设置中断描述符

// index   第index个描述符，index=0, 1, ..., 255

// address 中断处理程序的起始地址

// DPL     中断描述符的特权级

void InterruptManager::setInterruptDescriptor(uint32 index, uint32 address, byte DPL)

{

    IDT[index * 2] = (CODE_SELECTOR << 16) | (address & 0xffff);

    IDT[index * 2 + 1] = (address & 0xffff0000) | (0x1 << 15) | (DPL << 13) | (0xe << 8);

}